<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Sets a callback for specified URI</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="eventhttp.setallowedmethods.html">EventHttp::setAllowedMethods</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="eventhttp.setdefaultcallback.html">EventHttp::setDefaultCallback</a></div>
 <div class="up"><a href="class.eventhttp.html">EventHttp</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="eventhttp.setcallback" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">EventHttp::setCallback</h1>
  <p class="verinfo">(PECL event &gt;= 1.4.0-beta)</p><p class="refpurpose"><span class="refname">EventHttp::setCallback</span> &mdash; <span class="dc-title">Sets a callback for specified URI</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-eventhttp.setcallback-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span>
   <span class="type"><span class="type void">void</span></span> 
   <span class="methodname"><strong>EventHttp::setCallback</strong></span>
    ( <span class="methodparam">
    <span class="type">string</span>
     <code class="parameter">$path</code>
   </span>
   , <span class="methodparam">
    <span class="type">string</span>
     <code class="parameter">$cb</code>
   </span>
   [, <span class="methodparam">
    <span class="type">string</span>
     <code class="parameter">$arg</code>
   </span>
  ] )</div>

  <p class="para rdfs-comment">
   Sets a callback for specified URI.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-eventhttp.setcallback-parameters">
  <h3 class="title">参数</h3>
  <dl>

   
    <dt>

     <em><code class="parameter">path</code></em>
    </dt>

    <dd>

     <p class="para">
      The path for which to invoke the callback.
     </p>
    </dd>

   
   
    <dt>

     <em><code class="parameter">cb</code></em>
    </dt>

    <dd>

     <p class="para">
      The callback
      <span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span>
      that gets invoked on requested
      <em><code class="parameter">path</code></em>
      . It should match the following prototype:
     </p>
     <div class="methodsynopsis dc-description">
      <span class="type"><span class="type void">void</span></span> 
      <span class="methodname"><strong>callback</strong></span>
       ([ <span class="methodparam">
       <span class="type"><a href="class.eventhttprequest.html" class="type EventHttpRequest">EventHttpRequest</a></span>
        <code class="parameter">$req</code>
       <span class="initializer"> = NULL</span>
      </span>
      [, <span class="methodparam">
       <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span>
        <code class="parameter">$arg</code>
       <span class="initializer"> = NULL</span>
      </span>
     ]] )</div>

     <p class="para">
      <dl>

       
        <dt>

         <em><code class="parameter">req</code></em>
        </dt>

        <dd>

         <p class="para">
          <a href="class.eventhttprequest.html" class="classname">EventHttpRequest</a>
          object.
         </p>
        </dd>

       
       
        <dt>

         <em><code class="parameter">arg</code></em>
        </dt>

        <dd>

         <p class="para">
          Custom data.
         </p>
        </dd>

       
      </dl>

     </p>
    </dd>

   
   
    <dt>

     <em><code class="parameter">arg</code></em>
    </dt>

    <dd>

     <p class="para">
      Custom data.
     </p>
    </dd>

   
  </dl>

 </div>

 <div class="refsect1 returnvalues" id="refsect1-eventhttp.setcallback-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns <strong><code>TRUE</code></strong> on success. Otherwise <strong><code>FALSE</code></strong>.
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-eventhttp.setcallback-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-4174">
   <p><strong>Example #1 
    <span class="function"><strong>EventHttp::setCallback()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*<br />&nbsp;*&nbsp;Simple&nbsp;HTTP&nbsp;server.<br />&nbsp;*<br />&nbsp;*&nbsp;To&nbsp;test&nbsp;it:<br />&nbsp;*&nbsp;1)&nbsp;Run&nbsp;it&nbsp;on&nbsp;a&nbsp;port&nbsp;of&nbsp;your&nbsp;choice,&nbsp;e.g.:<br />&nbsp;*&nbsp;$&nbsp;php&nbsp;examples/http.php&nbsp;8010<br />&nbsp;*&nbsp;2)&nbsp;In&nbsp;another&nbsp;terminal&nbsp;connect&nbsp;to&nbsp;some&nbsp;address&nbsp;on&nbsp;this&nbsp;port<br />&nbsp;*&nbsp;and&nbsp;make&nbsp;GET&nbsp;or&nbsp;POST&nbsp;request(others&nbsp;are&nbsp;turned&nbsp;off&nbsp;here),&nbsp;e.g.:<br />&nbsp;*&nbsp;$&nbsp;nc&nbsp;-t&nbsp;127.0.0.1&nbsp;8010<br />&nbsp;*&nbsp;POST&nbsp;/about&nbsp;HTTP/1.0<br />&nbsp;*&nbsp;Content-Type:&nbsp;text/plain<br />&nbsp;*&nbsp;Content-Length:&nbsp;4<br />&nbsp;*&nbsp;Connection:&nbsp;close<br />&nbsp;*&nbsp;(press&nbsp;Enter)<br />&nbsp;*<br />&nbsp;*&nbsp;It&nbsp;will&nbsp;output<br />&nbsp;*&nbsp;a=12<br />&nbsp;*&nbsp;HTTP/1.0&nbsp;200&nbsp;OK<br />&nbsp;*&nbsp;Content-Type:&nbsp;text/html;&nbsp;charset=ISO-8859-1<br />&nbsp;*&nbsp;Connection:&nbsp;close<br />&nbsp;*<br />&nbsp;*&nbsp;3)&nbsp;See&nbsp;what&nbsp;the&nbsp;server&nbsp;outputs&nbsp;on&nbsp;the&nbsp;previous&nbsp;terminal&nbsp;window.<br />&nbsp;*/<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">_http_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$req</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;</span><span style="color: #0000BB">$counter&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;</span><span style="color: #0000BB">$max_requests&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(++</span><span style="color: #0000BB">$counter&nbsp;</span><span style="color: #007700">&gt;=&nbsp;</span><span style="color: #0000BB">$max_requests</span><span style="color: #007700">)&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Counter&nbsp;reached&nbsp;max&nbsp;requests&nbsp;</span><span style="color: #0000BB">$max_requests</span><span style="color: #DD0000">.&nbsp;Exiting\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">__METHOD__</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"&nbsp;called\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"request:"</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$req</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"data:"</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"\n=====&nbsp;DUMP&nbsp;=====\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Command:"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getCommand</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"URI:"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getUri</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Input&nbsp;headers:"</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getInputHeaders</span><span style="color: #007700">());<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Output&nbsp;headers:"</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getOutputHeaders</span><span style="color: #007700">());<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"\n&nbsp;&gt;&gt;&nbsp;Sending&nbsp;reply&nbsp;..."</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">sendReply</span><span style="color: #007700">(</span><span style="color: #0000BB">200</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"OK"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"OK\n"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"\n&nbsp;&gt;&gt;&nbsp;Reading&nbsp;input&nbsp;buffer&nbsp;...\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$buf&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getInputBuffer</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(</span><span style="color: #0000BB">$s&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$buf</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">readLine</span><span style="color: #007700">(</span><span style="color: #0000BB">EventBuffer</span><span style="color: #007700">::</span><span style="color: #0000BB">EOL_ANY</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$s</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"No&nbsp;more&nbsp;data&nbsp;in&nbsp;the&nbsp;buffer\n"</span><span style="color: #007700">;<br />}<br /><br />function&nbsp;</span><span style="color: #0000BB">_http_about</span><span style="color: #007700">(</span><span style="color: #0000BB">$req</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">__METHOD__</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"URI:&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getUri</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"\n&nbsp;&gt;&gt;&nbsp;Sending&nbsp;reply&nbsp;..."</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">sendReply</span><span style="color: #007700">(</span><span style="color: #0000BB">200</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"OK"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"OK\n"</span><span style="color: #007700">;<br />}<br /><br />function&nbsp;</span><span style="color: #0000BB">_http_default</span><span style="color: #007700">(</span><span style="color: #0000BB">$req</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">__METHOD__</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"URI:&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getUri</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"\n&nbsp;&gt;&gt;&nbsp;Sending&nbsp;reply&nbsp;..."</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">sendReply</span><span style="color: #007700">(</span><span style="color: #0000BB">200</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"OK"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"OK\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$port&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">8010</span><span style="color: #007700">;<br />if&nbsp;(</span><span style="color: #0000BB">$argc&nbsp;</span><span style="color: #007700">&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$port&nbsp;</span><span style="color: #007700">=&nbsp;(int)&nbsp;</span><span style="color: #0000BB">$argv</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">];<br />}<br />if&nbsp;(</span><span style="color: #0000BB">$port&nbsp;</span><span style="color: #007700">&lt;=&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">||&nbsp;</span><span style="color: #0000BB">$port&nbsp;</span><span style="color: #007700">&gt;&nbsp;</span><span style="color: #0000BB">65535</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;exit(</span><span style="color: #DD0000">"Invalid&nbsp;port"</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$base&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EventBase</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$http&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EventHttp</span><span style="color: #007700">(</span><span style="color: #0000BB">$base</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$http</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setAllowedMethods</span><span style="color: #007700">(</span><span style="color: #0000BB">EventHttpRequest</span><span style="color: #007700">::</span><span style="color: #0000BB">CMD_GET&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">EventHttpRequest</span><span style="color: #007700">::</span><span style="color: #0000BB">CMD_POST</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$http</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setCallback</span><span style="color: #007700">(</span><span style="color: #DD0000">"/dump"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"_http_dump"</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">8</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$http</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setCallback</span><span style="color: #007700">(</span><span style="color: #DD0000">"/about"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"_http_about"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$http</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setDefaultCallback</span><span style="color: #007700">(</span><span style="color: #DD0000">"_http_default"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"custom&nbsp;data&nbsp;value"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$http</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bind</span><span style="color: #007700">(</span><span style="color: #DD0000">"0.0.0.0"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">8010</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$base</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">loop</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
a=12
HTTP/1.0 200 OK
Content-Type: text/html; charset=ISO-8859-1
Connection: close
</pre></div>
   </div>
  </div>
 </div>

 <div class="refsect1 seealso" id="refsect1-eventhttp.setcallback-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member">
    <span class="methodname"><a href="eventhttp.setdefaultcallback.html" class="methodname" rel="rdfs-seeAlso">EventHttp::setDefaultCallback()</a> - Sets default callback to handle requests that are not caught by specific callbacks</span>
   </li>
  </ul>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="eventhttp.setallowedmethods.html">EventHttp::setAllowedMethods</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="eventhttp.setdefaultcallback.html">EventHttp::setDefaultCallback</a></div>
 <div class="up"><a href="class.eventhttp.html">EventHttp</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
